/*
 *  This file is part of WWWPal.
 *
 *  Copyright (C) 2003  Dr. John Punin, Department of Computer Science
 *  Rensselaer Polytechnic Institute
 *
 *  WWWPal is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  WWWPal is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "rep_logs_dates.h"
#include "rep_logs_pages.h"
#include "rep_general.h"
#include "GR_write_html.h"

void
write_body_date_html_file(FILE *fp,WebStatPtr msn);

int 
report_date_statistics(char *name_file,WebStatPtr msn)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Date Statistics");
  write_start_body_html_file(fp,"#ffffff");
  write_body_date_html_file(fp,msn);
  write_end_body_html_file(fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
write_body_date_html_file(FILE *fp,WebStatPtr msn)
{
  int i=0;
  time_t now;

  long thits = msn ? LOGR_get_total_site_naccesses(msn) : 0;
  double tsize = msn ? LOGR_get_total_site_size(msn) : 0;

  if(!msn || !fp)
      return;

  now = time(NULL);
  write_header_html_file(fp,1,"Date Statistics");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_bold_html_file(fp,"Time Periods : ");
  write_break_html_file(fp);
  write_time_periods(fp,msn);
  write_bold_html_file(fp,"Total Number of Requests : ");
  fprintf(fp, " %ld \n",thits);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Total Size : ");
  fprintf(fp, " %.2f K Bytes\n",(float)(tsize/1024.0));
  write_horizontal_rule_html_file(fp);
  fprintf(fp,RSDString); 
  write_horizontal_rule_html_file(fp);

  write_header_html_file(fp,2,"Statistics by Month");
  write_start_simple_table_html_file(fp);
  write_header_table_string3(fp,"Month", "Hits","HTML Pages","Bytes");

  for(i = 0; i < 12 ; i++)
  {
    WebGenInfoPtr wgi = (WebGenInfoPtr)LOGR_get_info_months(msn,i);
    if(wgi)
    {
      int nacc = (int)LOGR_get_gen_info_naccesses(wgi);
      int nhtml = (int)LOGR_get_gen_info_npages(wgi);
      double bytes = LOGR_get_gen_info_sizebytes(wgi);
      char sacc[20],shtml[20],sbytes[20];
      const char *smonth = LOGR_string_month(i);
      sprintf(sacc,"%d",nacc);
      sprintf(shtml,"%d",nhtml);
      sprintf(sbytes,"%.2f K",bytes/1024.0);
      if(smonth)
	  write_row_num_string3(fp,smonth,sacc,shtml,sbytes);
    }	
  }
  write_end_table_html_file(fp);


  write_header_html_file(fp,2,"Statistics by Day");
  write_start_simple_table_html_file(fp);
  write_header_table_string3(fp,"Day", "Hits","HTML Pages","Bytes");

  for(i = 0; i < 31 ; i++)
  {
    WebGenInfoPtr wgi = (WebGenInfoPtr)LOGR_get_info_days(msn,i);
    if(wgi)
    {
      int nacc = (int)LOGR_get_gen_info_naccesses(wgi);
      int nhtml = (int)LOGR_get_gen_info_npages(wgi);
      double bytes = LOGR_get_gen_info_sizebytes(wgi);
      char snum[20],sacc[20],shtml[20],sbytes[20];
      sprintf(snum,"%d",i+1);
      sprintf(sacc,"%d",nacc);
      sprintf(shtml,"%d",nhtml);
      sprintf(sbytes,"%.2f K",bytes/1024.0);
      write_row_num_string3(fp,snum,sacc,shtml,sbytes);
    }	
  }
  write_end_table_html_file(fp);


  write_header_html_file(fp,2,"Statistics by Hour");
  write_start_simple_table_html_file(fp);
  write_header_table_string3(fp,"Hour", "Hits","HTML Pages","Bytes");

  for(i = 0; i < 24 ; i++)
  {
    WebGenInfoPtr wgi = (WebGenInfoPtr)LOGR_get_info_hours(msn,i);
    if(wgi)
    {
      int nacc = (int)LOGR_get_gen_info_naccesses(wgi);
      int nhtml = (int)LOGR_get_gen_info_npages(wgi);
      double bytes = LOGR_get_gen_info_sizebytes(wgi);
      char snum[20],sacc[20],shtml[20],sbytes[20];
      sprintf(snum,"%d",i);
      sprintf(sacc,"%d",nacc);
      sprintf(shtml,"%d",nhtml);
      sprintf(sbytes,"%.2f K",bytes/1024.0);
      write_row_num_string3(fp,snum,sacc,shtml,sbytes);
    }	
  }
  write_end_table_html_file(fp);
}


